package com.thingsgrid.firmware.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thingsgrid.appmanager.util.GetID;
import com.thingsgrid.firmware.entity.FirmwareUpgradeMode;
import com.thingsgrid.firmware.entity.FirmwareUpgradeRecord;
import com.thingsgrid.firmware.service.IBatchupgradeService;
import lombok.AllArgsConstructor;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
public class BatchupgradeServiceImpl implements IBatchupgradeService {

	private MongoTemplate template;

	@Override
	public List<Map> getProduct() {
		Query query = new Query();
		query.fields().include("product_id");
		query.fields().include("product_name");
		query.fields().exclude("_id");
		return template.find(query, Map.class, "product");
	}

	@Override
	public List<Map> getFirmversion(String product_id, String upgrade_type) {
		Query query = null;
		if (upgrade_type.equals("整包升级")) {
			query = new Query(Criteria.where("product_id").is(product_id).and("stats").is(1).and("upgrade_type").is(0));
		} else {
			query = new Query(Criteria.where("product_id").is(product_id).and("stats").is(1).and("upgrade_type").is(1));
		}
		query.fields().include("firmware_id");
		query.fields().include("firmware_name");
		query.fields().include("firmware_version");
		query.fields().exclude("_id");
		return template.find(query, Map.class, "firmware");
	}

	@Override
	public List<Map> getWantedVersion(@RequestParam("firmware_id") String product_id, String upgrade_type, @RequestParam("firmware_version") String firmware_version) {

		Query query = null;
		if (upgrade_type.equals("整包升级")) {
			query = new Query(Criteria.where("product_id").is(product_id).and("stats").is(1).and("firmware_version").ne(firmware_version).and("upgrade_type").is(0));
		} else {
			query = new Query(Criteria.where("product_id").is(product_id).and("stats").is(1).and("firmware_version").ne(firmware_version).and("upgrade_type").is(1));
		}
		query.fields().include("firmware_id");
		query.fields().include("firmware_name");
		query.fields().include("firmware_version");
		query.fields().exclude("_id");

		return template.find(query, Map.class, "firmware");
	}

	@Override
	public Map<String, Object> getDeviceList(Page page, String product_id, String firmware_version) {
		Query query = new Query(Criteria.where("product_id").is(product_id).and("stats").is(1).and("firmware_version").is(firmware_version));
		Query pagequery = new Query(Criteria.where("product_id").is(product_id).and("stats").is(1).and("firmware_version").is(firmware_version)).skip((page.getCurrent() - 1) * page.getSize()).limit((int) page.getSize());
		pagequery.fields().include("device_id");
		pagequery.fields().include("device_name");
		pagequery.fields().exclude("_id");
		int count = (int) template.count(query, Map.class, "device");

		Map<String, Object> map = new HashMap<>();
		map.put("total", count);
		map.put("device", template.find(pagequery, Map.class, "device"));
		return map;
	}

	@Override
	public FirmwareUpgradeMode saveUpgradeMetnod(FirmwareUpgradeMode firmwareUpgradeMode) {
		FirmwareUpgradeMode firm = new FirmwareUpgradeMode();

		firm = template.insert(firmwareUpgradeMode, "firmware_upgrade_mode");
		return firm;

	}

	@Override
	public void insertFirmwareRecord(FirmwareUpgradeMode flag, String oldversion) {

		List<String> result = Arrays.asList(flag.getDeviceId().split(","));

		//-1 待升级 0 升级中 1 完成 2 失败
		for (String str : result) {
			FirmwareUpgradeRecord record = new FirmwareUpgradeRecord();
			record.setFirmwareId(flag.getFirmwareId());
			record.setOldFirmwareVersion(oldversion);
			record.setNewFirmwareVersion(flag.getNewFirmwareversion());
			GetID getid = new GetID();
			record.setUpgradeId(getid.getTemp());
			record.setMethodsId(flag.getMethodId());
			record.setUpgradeTime(flag.getUpgradePlanTime());
			record.setDeviceId(str);
			record.setUpgradeDepartment("");
			record.setUpgradeAuthor("");
			record.setUpgradeStats(-1);
			template.insert(record, "firmware_upgrade_record");
		}
	}

	@Override
	public List getDeviceList(String product_id, String firmware_version,List<String> devices) {
		Query query = new Query(Criteria.where("product_id").is(product_id).and("stats").is(1).and("firmware_version").is(firmware_version).and("device_id").nin(devices));
		query.fields().include("device_id");
		query.fields().include("device_name");
		query.fields().exclude("_id");

		return template.find(query, Map.class, "device");
	}

	@Override
	public List finddevice(String firmware_id, String oldversion, String newversion) {
		// -1 待升级  0 升级中 1 升级完成
		Query query = new Query(Criteria.where("firmware_id").is(firmware_id).and("upgrade_stats").ne(1).and("old_firmware_version").is(oldversion).and("new_firmware_version").is(newversion));
		query.fields().include("device_id");
		query.fields().exclude("_id");
		return template.find(query, Map.class, "firmware_upgrade_record");
	}
}
